# coding:utf-8
# author:frank
# date:2024/12/31
import pygame

pygame.init()

win_w=500
win_h=600
win = pygame.display.set_mode((win_w,win_h))
pygame.display.set_caption("按钮点击")
win.fill('white')
pygame.display.flip()


# 确定按钮和取消按钮
rect1 = pygame.draw.rect(win,'brown',(200,100,100,50))
rect2 = pygame.draw.rect(win,'purple',(200,200,100,50))

# 文字渲染到两个矩形中间
font = pygame.font.Font('files/Serendity.ttf',25)

text1 = font.render('OK',True,'#000000')
text2 = font.render('Cancel',True,'#000000')
w1,h1=text1.get_size()
w2,h2=text2.get_size()
win.blit(text1,(200+(100-w1)/2,100+(50-h1)/2))
win.blit(text2,(200+(100-w1)/2,200+(50-h1)/2))
pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            x,y=event.pos
            if 200<=x<=300 and 100<=y<=150:
                print("点击了确定按钮")
                # 在按钮原位置绘制灰色矩形覆盖原图
                pygame.draw.rect(win, 'grey', (200, 100, 100, 50))
                win.blit(text1, (200 + (100 - w1) / 2, 100 + (50 - h1) / 2))
                pygame.display.update()
            if 200 <= x <= 300 and 200 <= y <= 250:
                print("点击了取消按钮")
                pygame.draw.rect(win, 'grey', (200,200,100,50))
                win.blit(text2,(200+(100-w1)/2,200+(50-h1)/2))
                pygame.display.update()
        if event.type == pygame.MOUSEBUTTONUP:
            x, y = event.pos
            if 200 <= x <= 300 and 100 <= y <= 150:
                pygame.draw.rect(win, 'brown', (200, 100, 100, 50))
                win.blit(text1, (200 + (100 - w1) / 2, 100 + (50 - h1) / 2))
                pygame.display.update()
            if 200 <= x <= 300 and 200 <= y <= 250:
                pygame.draw.rect(win, 'purple', (200, 200, 100, 50))
                win.blit(text2,(200+(100-w1)/2,200+(50-h1)/2))
                pygame.display.update()